package com.app.actions.marketprocess;

import com.app.SpringAnnotationDef;
import com.app.actions.BaseAction;
import com.app.model.Goods_Package;
import com.app.model.Order_Info;
import com.app.model.User;
import com.app.service.marketprocess.OrderServices;
import com.app.utils.OrderState;
import com.app.utils.SerialUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-27
 * Time: 下午3:24
 * To change this template use File | Settings | File Templates.
 */

@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Component
@Namespace("/order")
public class OrderAction extends BaseAction {
    private static final Logger LOGGER = Logger.getLogger(OrderAction.class);


    @Resource(name = SpringAnnotationDef.ORDERINFO_SERVICE)
    private OrderServices services;

    /**
     * 订单表
     */
    private Order_Info orderInfo;

    public Order_Info getOrderInfo() {
        return orderInfo;
    }

    public void setOrderInfo(Order_Info orderInfo) {
        this.orderInfo = orderInfo;
    }

    @Action(value = "order", results = {@Result(name = "menuInfo", location = "/basesetting/goods/getSystemCfg.jsp"),
            @Result(name = "toAddOrder", location = "/marketprocess/appointment/addOrderInfo.jsp"),
            @Result(name = "addGoodsInfo", location = "/basesetting/goods/addGoodsInfo.jsp"),
            @Result(name = SUCCESS, location = "/basesetting/goods/list.jsp")})
    public String getGoods() {
        return SUCCESS;
    }


    public String toAddOrder() {
        //1.自动生成订单号
        String seriaId = SerialUtils.getSerialNumber();
        getRequest().setAttribute("seriaId", seriaId);

        services.getUserDao().listAll();
        return "toAddOrder";
    }

    /**
     * 添加一个订单
     */
    public void addOrder() {
        if (orderInfo != null) {
            User user = getSessionLoginedUser();
            orderInfo.setInputId(user.getId());
            orderInfo.setInputName(user.getEname());

            orderInfo.setMenShiId(user.getId());
            orderInfo.setMenShiName(user.getEname());

            Goods_Package gpk = new Goods_Package();
            gpk.setId(orderInfo.getPackageId());
            gpk = services.getGoodsDao().getGoodsPackageById(gpk);

            //保存套餐的名字
            if (gpk != null) {
                orderInfo.setPackageName(gpk.getPackageName());
            }
            //判断是预约的 但是单项的       流程 or 单项
            orderInfo.setOrderSource("流程");
            orderInfo.setCreateDate(new Date());
            orderInfo.setOrderState(OrderState.OrderState10);   //刚下订单

            services.getDao().saveObject(orderInfo);

            JsonOut("添加成功");
        } else {
            JsonOut("添加失败");
        }
    }

}
